#!/bin/bash
[[ "${DEBUG}" == "true" ]] && set -x
red="\e[31m"
green="\e[32m"
light_green="\e[32;1m"
blue="\e[34m"
purple="\e[35m"
cyan="\e[36m"
nocolor="\e[0m"
yellow="\e[33;1m"

Info="${green}[信息]${nocolor}"
Error="${red}[错误]${nocolor}"
Tip="${purple}[注意]${nocolor}"
Warn="${yellow}[警告]${nocolor}"


function menu() {
options=
while [[ ! $options =~ ^[1-9]+[0-9]*$ ]] || [[ $options -gt 18 ]]
do
clear
echo -ne "系统优化及服务安装脚本 ${red}[v1.0.0]${nocolor}
-- 系统利刃 | Author: Elvis --
----------------------
 ${green}1.${nocolor}  优化系统
 ${green}2.${nocolor}  关闭服务
 ${green}3.${nocolor}  安装MySQL
 ${green}4.${nocolor}  安装Redis
 ${green}5.${nocolor}  安装Memcached
 ${green}6.${nocolor}  安装Nginx
 ${green}7.${nocolor}  安装Apache
 ${green}8.${nocolor}  安装PHP
 ${green}9.${nocolor}  安装JDK
 ${green}10.${nocolor} 安装Jenkins
 ${green}11.${nocolor} 安装Nexus
 ${green}12.${nocolor} 安装SVN
 ${green}13.${nocolor} 安装Docker
 ${green}14.${nocolor} 配置本地YUM源
 ${green}15.${nocolor} 自签名SSL证书
 ${green}16.${nocolor} 修复CentOS6 YUM源
 ${green}17.${nocolor} 查看说明
 ${green}18.${nocolor} 退出
-----------------------

请输入数字 [1-18]: "
read options
if [[ ! $options =~ ^[1-9]+[0-9]*$ ]] || [[ $options -gt 18 ]];then
   echo -e "$Error 请重新输入1-18的数字"
   sleep 1.5
fi
echo -e ""
done
case $options in
"1")
    clear
    source $workHome/include/sysOptimizec.sh
    sys_optimize |tee -a $logs_file
    menu
    ;;
"2")
    clear
    source $workHome/include/serviceOptimizec.sh
    srv_optimize |tee -a $logs_file
    menu
    ;;
"3")
    clear
    source $workHome/include/mysqlc.sh
    install_mysql |tee -a $logs_file
    menu
    ;;
"4")
    clear
    source $workHome/include/redisc.sh
    install_redis |tee -a $logs_file
    menu
    ;;    
"5")
    clear
    source $workHome/include/memcachedc.sh
    install_memcached |tee -a $logs_file
    menu
    ;;
"6")
    clear
    source $workHome/include/nginxc.sh
    install_nginx |tee -a $logs_file
    menu
    ;;
"7")
    clear
    source $workHome/include/apachec.sh
    install_apache |tee -a $logs_file
    menu
    ;;
"8")
    clear
    source $workHome/include/phpc.sh
    install_php |tee -a $logs_file
    menu
    ;;
"9")
    clear
    source $workHome/include/jdkc.sh
    install_jdk |tee -a $logs_file
    menu
    ;;
"10")
    clear
    source $workHome/include/jenkinsc.sh
    install_jenkins |tee -a $logs_file
    menu
    ;;
"11")
    clear
    source $workHome/include/nexusc.sh
    install_nexus |tee -a $logs_file
    menu
    ;;
"12")
    clear
    source $workHome/include/csvnc.sh
    install_csvn |tee -a $logs_file
    menu
    ;;
"13")
    clear
    source $workHome/include/dockerc.sh
    install_docker |tee -a $logs_file
    menu
    ;;
"14")
    clear
    source $workHome/include/localyumc.sh
    yum_options |tee -a $logs_file
    menu
    ;;
"15")
    clear
    source $workHome/include/sslcertc.sh
    create_ssl_cert |tee -a $logs_file
    menu
    ;;
"16")
    clear
    source $workHome/include/yumRepairc.sh
    yum_repair |tee -a $logs_file
    menu
    ;;
"17")
    clear
    source $workHome/include/readmec.sh
    readme
    menu
    ;;
"18")
    echo -e "$Info 退出脚本中......\n"
    sleep 0.30
    exit 0
    ;;
esac
}

function anykeyback() {
echo ""
echo -ne "${cyan}按任意键返回${nocolor}"
read -s -n1 goback
sleep 0.15
}

#脚本位置
workHome=`pwd`


#判断系统版本
if [ -s /etc/redhat-release ]; then
     system_ver=`grep -oE  "[0-9.]+" /etc/redhat-release | cut -d . -f 1`
else
     system_ver=`grep -oE  "[0-9.]+" /etc/issue | cut -d . -f 1`
     echo -e "$Error 不支持的操作系统"
     anykeyback
     return 1
fi


#判断系统能否联网
echo -e "${green}正在检测系统环境，请稍等.....$nocolor"
ping -c 3 -w 6 223.6.6.6 >/dev/null 2>&1
if [ "$?" != "0" ];then
   die_network="true"
else
   die_network="false"
fi

echo $0|grep -w "env.sh" >/dev/null 2>&1
if [ "$?" == "0" ];then
   whiptail -h>/dev/null 2>&1
   if [ "$?" != "0" ];then
      echo "安装whiptail" > /$workHome/logs/init.log
      if [ "$die_network" == "true" ];then
         echo -e "\033[31m$check_newt\033[0m"
         exit 1
      else
         echo -e "\033[33m$install_newt\033[0m"
         yum -y install newt qrencode>>/$workHome/logs/init.log 2>&1
      fi
   fi
fi
#二维码展示
echo "安装二维码生成软件,非必须，YUM安装有2分钟超时设置" >>/$workHome/logs/init.log
rpm -q qrencode >/dev/null 2>&1
if [ "$?" != "0" ];then
   rpm -ivh packages/extra/el${system_ver}/libpng*.rpm >>/$workHome/logs/init.log 2>&1
   rpm -ivh packages/extra/el${system_ver}/qrencode-libs*.rpm >>/$workHome/logs/init.log 2>&1
   rpm -ivh packages/extra/el${system_ver}/qrencode-[0-9]*.rpm >>/$workHome/logs/init.log 2>&1
fi

rpm -q qrencode >/dev/null 2>&1
if [ "$?" != "0" -a  "$die_network" = "false" ];then
   yum -y install epel-release >>/$workHome/logs/init.log 2>&1 &
   epelpid=$!
   sleep 60
   kill $epelpid   
   yum -y install qrencode >>/$workHome/logs/init.log 2>&1 &
   yumpid=$!
   sleep 60
   kill $yumpid
fi


#主机IP获取
netdev=`ip route|grep "^default"|awk '{print $5}'`
ipaddr=`ip address show $netdev|grep "inet "|awk -F"/" '{print $1}'|awk '{print $2}'`

#日志记录文件
logs_file=/$workHome/logs/env.log.`date "+%Y-%m-%d_%H%M%S"`
echo "欢迎使用使用env.sh脚本" > $logs_file

menu

